/*
 * Generated on Apr 17, 2005
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package cgl.narada.service.record;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import cgl.narada.event.TemplateProfileAndSynopsisTypes;

/**
 * @author hbulut
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class RecordRequestEvent implements RecordRequest {

  private int     requestType            = RecordServiceExchange.RECORD_SERVICE_REQUEST_REGISTER;
  private boolean hasRequestIdentifier   = false;
  private String  requestIdentifier      = null;
  private boolean hasReplyTopic          = false;
  private String  replyTopic             = null;
  private int     entityId               = 0;
  private int     templateId             = 0;
  private boolean hasContentSynopsisType = true;
  private int     contentSynopsisType    = TemplateProfileAndSynopsisTypes.STRING;
  private boolean hasContentSynopsis     = false;
  private String  contentSynopsis        = null;

  private String  moduleName             = "RecordRequestEvent: ";
  
  public RecordRequestEvent(byte[] marshalledBytes) {
    ByteArrayInputStream baInputStream = new ByteArrayInputStream(
        marshalledBytes);
    DataInputStream din = new DataInputStream(baInputStream);
    try {
      din.readByte();
      /** RecordServiceExchange.RECORD_SERVICE_REQUEST */

      requestType = din.readInt();
      /** Register or Deregister */

      hasRequestIdentifier = din.readBoolean();
      if (hasRequestIdentifier) {
        byte[] idBytes = new byte[din.readInt()];
        din.readFully(idBytes);
        requestIdentifier = new String(idBytes);
      }

      hasReplyTopic = din.readBoolean();
      if (hasReplyTopic) {
        byte[] tpcBytes = new byte[din.readInt()];
        din.readFully(tpcBytes);
        replyTopic = new String(tpcBytes);
      }

      entityId = din.readInt();
      templateId = din.readInt();

      hasContentSynopsisType = din.readBoolean();
      if (hasContentSynopsisType) {
        contentSynopsisType = din.readInt();
      }

      hasContentSynopsis = din.readBoolean();
      if (hasContentSynopsis) {
        byte[] tpcBytes = new byte[din.readInt()];
        din.readFully(tpcBytes);
        contentSynopsis = new String(tpcBytes);
      }
    } catch (IOException ioe) {
      System.out.println(moduleName + "Error while un-marshalling");
    }
    /** end try-catch block */

  }

  public byte[] getBytes() {
    ByteArrayOutputStream baOutputStream = new ByteArrayOutputStream();
    DataOutputStream dout = new DataOutputStream(baOutputStream);
    byte[] marshalledBytes = null;
    try {
      dout.writeByte(RecordServiceExchange.RECORD_SERVICE_REQUEST);

      dout.writeInt(requestType);

      /** Deal with request identifier */
      dout.writeBoolean(hasRequestIdentifier);
      if (hasRequestIdentifier) {
        byte[] idBytes = requestIdentifier.getBytes();
        dout.writeInt(idBytes.length);
        dout.write(idBytes);
      }

      dout.writeBoolean(hasReplyTopic);
      if (hasReplyTopic) {
        byte[] objBytes = replyTopic.getBytes();
        dout.writeInt(objBytes.length);
        dout.write(objBytes);
      }

      dout.writeInt(entityId);
      dout.writeInt(templateId);

      dout.writeBoolean(hasContentSynopsisType);
      if (hasContentSynopsisType) {
        dout.writeInt(contentSynopsisType);
      }

      dout.writeBoolean(hasContentSynopsis);
      if (hasContentSynopsis) {
        byte[] objBytes = contentSynopsis.getBytes();
        dout.writeInt(objBytes.length);
        dout.write(objBytes);
      }
      marshalledBytes = baOutputStream.toByteArray();
    } catch (IOException ioe) {
      System.out.println(moduleName + "Error while marshalling");
    }
    /** end try-catch block */

    return marshalledBytes;
  }

  public RecordRequestEvent(int requestType, String replyTopic,
      String requestIdentifier, int entityId, int templateId,
      int contentSynopsisType, String contentSynopsis) {
    this.requestType = requestType;
    this.replyTopic = replyTopic;
    if (this.replyTopic != null) {
      this.hasReplyTopic = true;
    }
    this.requestIdentifier = requestIdentifier;
    if (this.requestIdentifier != null) {
      this.hasRequestIdentifier = true;
    }

    this.entityId = entityId;
    this.templateId = templateId;
    this.hasContentSynopsisType = true;
    this.contentSynopsisType = contentSynopsisType;
    this.contentSynopsis = contentSynopsis;
    if (this.contentSynopsis != null) {
      this.hasContentSynopsis = true;
    }
  }

  public String toString() {
    String stringRep = moduleName;
    stringRep += "\nrequestType=" + requestType;
    stringRep += "\nhasRequestIdentifier=" + hasRequestIdentifier;
    stringRep += "\nRequestIdentifier=" + requestIdentifier;
    stringRep += "\nhasReplyTopic=" + hasReplyTopic;
    stringRep += "\nReplyTopic=" + replyTopic;
    stringRep += "\nEntityId=" + entityId;
    stringRep += "\nTemplateId=" + templateId;

    stringRep += "\nhasContentSynopsisType=" + hasContentSynopsisType;
    stringRep += "\ncontentSynopsisType=" + contentSynopsisType;

    stringRep += "\nhasContentSynopsis=" + hasContentSynopsis;
    stringRep += "\ncontentSynopsis=" + contentSynopsis;
    return stringRep;
  }

  public static void main(String[] args) {
    RecordRequestEvent res = new RecordRequestEvent(
        RecordServiceExchange.RECORD_SERVICE_REQUEST_REGISTER, "/ReplyTopic",
        "/RequestIdentifier", 23, 34, 1, "/home/nb/recordtopic");
    System.out.println("req:\n" + res.toString());
    System.out.println("\n****************************\n");
    RecordRequestEvent res2 = new RecordRequestEvent(res.getBytes());
    System.out.println("req2:\n" + res2.toString());

  }
  
  
  public String getContentSynopsis() {
    return contentSynopsis;
  }
  public int getContentSynopsisType() {
    return contentSynopsisType;
  }
  public int getEntityId() {
    return entityId;
  }
  public boolean hasContentSynopsis() {
    return hasContentSynopsis;
  }
  public boolean hasContentSynopsisType() {
    return hasContentSynopsisType;
  }
  public boolean hasReplyTopic() {
    return hasReplyTopic;
  }
  public boolean hasRequestIdentifier() {
    return hasRequestIdentifier;
  }
  public String getModuleName() {
    return moduleName;
  }
  public String getReplyTopic() {
    return replyTopic;
  }
  public String getRequestIdentifier() {
    return requestIdentifier;
  }
  public int getRequestType() {
    return requestType;
  }
  public int getTemplateId() {
    return templateId;
  }
}